package com.hospital.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.hospital.dao.LogisticsDao;
import com.hospital.dto.LogisticsDto;
import com.hospital.dto.LogisticsStateDto;
import com.hospital.dto.LogisticsStateTjDto;
import com.hospital.entity.Admin;
import com.hospital.entity.Logistics;
import com.hospital.service.intf.LogisticsService;
import com.hospital.util.StringUtil;
import com.hospital.vo.PageBean;
import com.hospital.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class LogisticsServiceImpl implements LogisticsService {

    @Autowired
    private LogisticsDao dao;

    @Override
    public R add(LogisticsDto logisticsDto, HttpServletRequest request) {
        Admin admin = (Admin) request.getSession().getAttribute("admin");
        logisticsDto.setEmpid(admin.getId());
        if (dao.add(logisticsDto) > 0) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

    @Override
    public R update(LogisticsDto logisticsDto) {
        if (dao.update(logisticsDto) > 0) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

    @Override
    public R updateState(Integer id, Integer state) {
        if (dao.updateState(id, state) > 0) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

    @Override
    public R delete(Integer id) {
        if (dao.delete(id) > 0) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

    @Override
    public PageBean selectPage(String info, int page, int limit) {
        if (StringUtil.isNotEmpty(info)) {
            info = "%" + info + "%";
        }
        int start = (page - 1) * limit;

        return PageBean.ok(dao.selectCount(info), dao.selectPage(info, start, limit));
    }

    @Override
    public List<LogisticsDto> all() {
        return dao.all();
    }

    @Override
    public R upload(MultipartFile file) throws IOException {
        //1.接收文件， 获取文件内容
        //1.验证上传文件是否存在
        if (!file.isEmpty()) {
            //2.解析Excel -EasyExcel
            //2.解析Excel数据
            List<Logistics> list = EasyExcel.read(file.getInputStream(), Logistics.class, new ReadListener() {
                @Override
                public void invoke(Object o, AnalysisContext analysisContext) {
                }

                @Override
                public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                }
            }).sheet().doReadSync();
            //3.Mybatis 实现批量新增
            if (dao.saveBatch(list) > 0) {
                //4.返回
                return R.ok();
            }
        }
        return R.fail();
    }

    @Override
    public R delIds(int[] ids) {
        if (dao.delByIds(ids) > 0) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

    @Override
    public R statetj() {
        List<LogisticsStateDto> list = dao.selectState();
        List<LogisticsStateTjDto> result = new ArrayList<>();
        list.forEach(s -> {
            result.add(new LogisticsStateTjDto(parseState(s.getState()), s.getNum()));

        });
        result.forEach(s->{
            System.err.println(s.toString());
        });
        return R.ok(result);
    }


    private String parseState(int sex) {
        String r = "";
        switch (sex) {
            case 1:
                r = "暂未处理";
                break;
            case 2:
                r = "正在处理";
                break;
            default:
                r = "处理完毕";
                break;
        }
        return r;
    }

}
